package com.yc.fav.util;

import com.google.gson.Gson;
import com.yc.fav.service.BizException;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collection;
import java.util.Map;

public class Utils {
    public static void check(boolean bool,String msg,int code) throws BizException {
        if(bool){
            throw new BizException(msg,code);

        }
    }
    public static void checkEmpty(Object obj,String msg,int code) throws BizException {
        if(obj == null){
            throw new BizException(msg,code);

        } else if (obj instanceof String) {
            String s = (String) obj;
            if(s.isBlank()){
                throw new BizException(msg,code);
            }
        }else if (obj instanceof Map) {
            Map s = (Map) obj;
            if(s.isEmpty()){
                throw new BizException(msg,code);
            }
        }
    }
    public static void output(HttpServletResponse resp, Object data) throws IOException {
        resp.setContentType("application/json;charset=utf-8");
        String json = new Gson().toJson(data);
        resp.getWriter().println(json);
    }
}
